package com.github.sbb.sherry.primitives.rules;

import com.github.sbb.sherry.primitives.api.Facts;
import com.github.sbb.sherry.primitives.api.Rule;
import com.github.sbb.sherry.primitives.api.RuleListener;
import com.github.sbb.sherry.primitives.api.Rules;
import com.github.sbb.sherry.primitives.api.StatelessRulesEngine;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class DefaultStatelessRulesEngine implements StatelessRulesEngine {

  private static final Logger logger = LoggerFactory.getLogger(DefaultStatelessRulesEngine.class);

  public DefaultStatelessRulesEngine() {
    super();
  }

  @Override
  public void fire(Rules rules, Facts facts) {
    fire(rules, facts, null);
  }

  @Override
  public void fire(Rules rules, Facts facts, List<RuleListener> ruleListeners) {
    if (rules.isEmpty()) {
      return;
    }
    for (Rule rule : rules) {
      boolean evaluationResult = rule.evaluate(facts);
      if (evaluationResult) {
        try {
          rule.execute(facts);
        } catch (Exception exception) {
        }
      }
    }
  }
}
